import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLOutput;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-09-15
 * Time: 10:31
 */
public class JDBCDemo1 {
    public static void main(String[] args) throws SQLException {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入学号");
        int id1=scanner.nextInt();
        System.out.println("请输入姓名");
        String name=scanner.next();
        System.out.println("请输入班级");
        int id2=scanner.nextInt();
        //1.创建数据源
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/d09073?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1111");
        //2.和数据库服务器建立连接
        Connection  connection=dataSource.getConnection();//这里要抛出异常
        //3.构造SQL语句
        String sql="insert into student values(?,?,?)";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setInt(1,id1);
        statement.setString(2,name);
        statement.setInt(3,id2);
        //4. 执行 SQL 语句~. 返回值就是 "这次操作影响到几行"
        int n=statement.executeUpdate();
        System.out.println("n="+n);
        // 5. 释放必要的资源. 关闭连接.
        statement.close();
        connection.close();
    }
}
